package com.xunyji.demo00testdemo.comm.util.weixin;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author AltEnter
 * @create 2019-01-12 15:01
 * @desc 授权工具类
 **/
@Slf4j
public class AuthUtil {

    /** 公众号appid */
    public static final String APPID = "wx223cd06f860141a8";
    /** 公众号appsecret */
    public static final String APPSECRET = "266b4e7fc93370adaf20baf49c0cbef1";

    /**
     * 发送get请求
     * @param url 目标url
     * @return
     * @throws IOException
     */
    public static JSONObject doGetJson(String url) throws IOException {
//        01 存放响应信息
        JSONObject jsonObject = null;
//        02 创建Http客户端
        DefaultHttpClient cleint = new DefaultHttpClient();
//        03 创建HttpGet对象
        HttpGet httpGet = new HttpGet(url);
//        03 利用HTTP客户端执行GET请求
        HttpResponse response = cleint.execute(httpGet);
//        04 从响应对象中获取响应数据
        HttpEntity entity = response.getEntity();
//        05 响应数据非空判断和格式转化
        if (entity != null) {
            String result = EntityUtils.toString(entity, "utf-8");
            jsonObject = JSONObject.fromObject(result);
        }
//        释放HttpGet对象
        httpGet.releaseConnection();
        return jsonObject;
    }
}

